﻿<template>
	<div class="mrsMedia-container">
		<el-card shadow="hover">
			<el-form :model="state.queryParams" ref="queryForm" :inline="true">
				<el-form-item :label-width='70' label="媒体名称" prop="name">
					<el-input placeholder="媒体名称" clearable class='ml10' @keyup.enter="handleQuery"
						v-model="state.queryParams.mediaName" />
				</el-form-item>
				<el-form-item>
					<el-button-group>
						<el-button type="primary" icon="ele-Search" @click="handleQuery" v-auth="'mrsMedia:query'"> 查询
						</el-button>
						<el-button icon="ele-Refresh" @click="resetQuery" v-auth="'mrsMedia:query'"> 重置 </el-button>
					</el-button-group>
				</el-form-item>
				<el-form-item>
					<el-button type="success" plain icon="ele-Plus" @click="openAddMrsMedia" v-auth="'mrsMedia:add'"> 新增
					</el-button>
				</el-form-item>
			</el-form>
		</el-card>

		<el-card shadow="hover" style="margin-top: 8px">
			<el-table :data="state.data" style="width: 100%" v-loading="state.loading" tooltip-effect="light" row-key="id"
				border stripe highlight-current-row>
				<el-table-column header-align="center" type="index" label="序号" width="55" align="center" />
				<el-table-column header-align="center" prop="mediaName" label="媒体名称" show-overflow-tooltip width="150" />
				<el-table-column header-align="center" prop="mediaCode" label="媒体代码" show-overflow-tooltip width="150" />
				<el-table-column header-align="center" prop="openUrl" label="外放Url" show-overflow-tooltip min-width="180" />
				<el-table-column header-align="center" prop="status" label="状态" show-overflow-tooltip width="80">
					<template #default="scope">
						<el-tag type="success" v-if="scope.row.status === 1">启用</el-tag>
						<el-tag type="danger" v-else-if="scope.row.status === 2">停用</el-tag>
						<el-tag type="info" v-else>未知</el-tag>
					</template>
				</el-table-column>
				<el-table-column header-align="center" prop="createTime" label="创建时间" show-overflow-tooltip width="160" />
				<el-table-column header-align="center" prop="remark" label="备注" show-overflow-tooltip/>
				<el-table-column label="操作" width="140" align="center" fixed="right" show-overflow-tooltip>
					<template #default="scope">
						<el-button icon="ele-Edit" size="small" text type="primary" @click="openEditMrsMedia(scope.row)"
							v-auth="'mrsMedia:update'"> 编辑 </el-button>
						<el-button icon="ele-Delete" size="small" text type="danger" @click="delMrsMedia(scope.row)"
							v-auth="'mrsMedia:delete'"> 删除 </el-button>
					</template>
				</el-table-column>
			</el-table>
			<el-pagination @size-change="onHandleSizeChange" @current-change="onHandleCurrentChange" class="mt15"
				:page-sizes="[20, 50, 100]" v-model:current-page="state.queryParams.pageNum" background
				v-model:page-size="state.queryParams.pageSize" layout="total, sizes, prev, pager, next, jumper"
				:total="state.total" />
			<EditMrsMedia ref="editMrsMediaRef" :title="state.editTitle" />
		</el-card>
	</div>
</template>

<script lang="ts" setup name="mrsMedia">
import { onMounted, onUnmounted, reactive, ref } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import mittBus from '/@/utils/mitt';
import EditMrsMedia from '/@/views/memberRights/operate/media/component/editMrsMedia.vue';

import { MrsMedia } from '/@/api/models';
import SysMrsMediaApi from '/@/api/memberRights/media';

// 定义变量内容
const editMrsMediaRef = ref<InstanceType<typeof EditMrsMedia>>();
const state = reactive({
	loading: false,
	data: [] as Array<MrsMedia>,
	total: 0 as any,
	queryParams: {
		mediaName: undefined,
		pageNum: 1,
		pageSize: 20,
	},
	editTitle: '',
});

onMounted(async () => {
	mittBus.on('submitRefresh', () => {
		handleQuery();
	});
});

onUnmounted(() => {
	mittBus.off('submitRefresh');
});

// 查询操作
const handleQuery = async () => {
	state.loading = true;
	const res = await SysMrsMediaApi().queryRecord(state.queryParams);
	state.data = res.data.result.items ?? [];
	state.total = res.data.result?.total;
	state.loading = false;
};

// 重置操作
const resetQuery = () => {
	state.queryParams.mediaName = undefined;
	handleQuery();
};

// 打开新增页面
const openAddMrsMedia = () => {
	state.editTitle = '添加';
	editMrsMediaRef.value?.openDialog({ status: 1 });
};

// 打开编辑页面
const openEditMrsMedia = (row: MrsMedia) => {
	state.editTitle = '编辑';
	editMrsMediaRef.value?.openDialog(row);
};

// 删除
const delMrsMedia = (row: any) => {
	ElMessageBox.confirm(`确定删除：【${row.name}】?`, '提示', {
		confirmButtonText: '确定',
		cancelButtonText: '取消',
		type: 'warning',
	})
		.then(async () => {
			let res = await SysMrsMediaApi().deletePost({ id: row.id });
			if (res.data.resCode != '0000')
				ElMessage.error(res.data.resMessage);
			else
				ElMessage.success("删除成功");
			await handleQuery();
		})
		.catch(() => { });
};

// 改变页面容量
const onHandleSizeChange = (val: number) => {
	state.queryParams.pageSize = val;
	handleQuery();
};

// 改变页码序号
const onHandleCurrentChange = (val: number) => {
	state.queryParams.pageNum = val;
	handleQuery();
};

</script>
